﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Wicresoft.IDE.Loader;

namespace Wicresoft.IDE.Shell
{
    public class RecentFileEventArgs : EventArgs
    {
        public RecentFileEventArgs(string fileName)
        {
            FileName = fileName;
        }

        public string FileName { get; set; }
    }

    public partial class StartPage : UserControl
    {
        public delegate void OpenRecentFileHandler(object sender, RecentFileEventArgs e);
        public event OpenRecentFileHandler RecentFileOpened;
        private void OpenRecentFile(RecentFileEventArgs e)
        {
            if (RecentFileOpened != null)
            {
                RecentFileOpened(this, e);
            }
        }

        public StartPage()
        {
            InitializeComponent();

            if (File.Exists("startpage.mht"))
                webBrowser1.Navigate(Environment.CurrentDirectory + "\\startpage.mht");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var ul = webBrowser1.Document.GetElementById("ulRecentFiles");

            //ul.InnerHtml = "<li><a title=\"c:\\page1.umc\">page222.umc</a></li>";

            if (ul == null)
                return;


            foreach (var s in RecentFileUtil.GetRecentFiles(6))
            {
                var li = webBrowser1.Document.CreateElement("li");
                li.SetAttribute("title", s);
                li.Style = "cursor: hand";
                li.InnerText = new FileInfo(s).Name;
                li.Click += new HtmlElementEventHandler(li_Click);
                ul.AppendChild(li);
            }
        }

        void li_Click(object sender, HtmlElementEventArgs e)
        {
            var li = sender as HtmlElement;
            OpenRecentFile(new RecentFileEventArgs(li.GetAttribute("title")));
        }
    }
}
